<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="文章名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入文章名称"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入文章作者"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="文章类型" prop="type">-->
<!--        <el-input-->
<!--          v-model="queryParams.type"-->
<!--          placeholder="请输入文章类型"-->
<!--          clearable-->
<!--          style="width: 200px"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="文章分类" >
        <el-select v-model="queryParams.type" placeholder="请选择文章分类" style="width:100%;">
          <el-option
            v-for="item in articletype"
            :key="item.id"
            :label="item.name"
            :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"

        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column label="文章编号" prop="articleId" width="150" />
      <el-table-column label="作者" prop="author" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="文章标题" prop="name" width="150" >
      </el-table-column>
      <el-table-column label="浏览量" prop="views" width="150" />
      <el-table-column label="文章类型" prop="type" width="150" />
      <el-table-column label="排序" prop="orderNum" width="150" />

      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.articleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-comment"
            @click="handleGetInfo(scope.row)"

          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框   -->
        <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body  custom-class="dialog-with-background">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-row>
                <el-row>
                  <el-col :span="24">
                    <el-form-item label="作者" prop="author">
                      <el-input v-model="form.author" placeholder="请输入作者名称" maxlength="30" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <!--          <el-form-item label="文章类型" prop="type">-->
                <!--              <el-input v-model="form.type" placeholder="请输入文章名称" />-->
                <!--          </el-form-item>-->
                <el-form-item label="文章分类" :rules="[{required:true, message:'请选择分类', trigger:['blur','change']}]">
                  <el-select v-model="form.type" placeholder="请选择文章分类" style="width:100%;">
                    <el-option
                      v-for="item in articletype"
                      :key="item.id"
                      :label="item.name"
                      :value="item.name">
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="简介" prop="description">
                  <el-input v-model="form.description" placeholder="请输入文章简介" />
                </el-form-item>
                <el-form-item label="浏览量" prop="views">
                  <el-input v-model="form.views" placeholder="请输入浏览量" />
                </el-form-item>


                <el-form-item label="排序" prop="orderNum">
                  <el-input v-model="form.orderNum" placeholder="请输入文章排序" />
                </el-form-item>

                <el-col :span="12">
                  <el-form-item label="文章题目" prop="name">
                    <el-input v-model="form.name" placeholder="请输入文章题目" />
                  </el-form-item>
                </el-col>
                  <el-col :span="24">
                    <el-form-item label="内容">
                      <editor class="editor"  v-model="form.content"  :min-height="192"/>
                    </el-form-item>
                  </el-col>

              </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
          </div>

        </el-dialog>



  </div>
</template>

<script>

import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
import {listClassify,addArticle, delArticle, getArticle,  listArticle, updateArticle} from "@/api/article/article";

export default {
  name: "article",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // categoryData: [],
      articleList: [],
      articletype: [],
      content: "",
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {


        pageNum: 1,
        pageSize: 10,
        author: undefined,
        name: undefined,
        type : undefined
        // status: undefined
      },
      queryParams1: {
        name: undefined,
        status: undefined
      },
      // 表单参数
      form: {
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        content:[
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        author: [
          { required: true, message: "作者不能为空", trigger: "blur" }
        ],

        description: [
          { required: false, message: "简介不能为空", trigger: "blur" }
        ],
        views: [
          { required: true, message: "浏览量不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "库存不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "顺序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.GetClassifyType();

  },

  methods: {
    /**
     * get classify type
     */

    GetClassifyType()
    {
      listClassify(this.queryParams1).then(response =>{
        for(let d of response.data)
        {
          if(d.parentId !=0&&d.status!=1)
          {
            this.articletype.push(d);
          }
        }
      });
    },
    /** 查询文章列表 */
      getList() {
      this.loading = true;
      listArticle(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.articleList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 查询菜单树结构 */
    getMenuTreeselect() {
      menuTreeselect().then(response => {
        this.menuOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
        this.form = {
          content : undefined,
          articleId: undefined,
          author: undefined,
          type: undefined,
          description: undefined,
          orderNum: undefined,
          views: undefined,
          status: "0",
        };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.getMenuTreeselect();
      this.open = true;
      this.title = "添加文章";
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const articleId = row.articleId || this.ids;
      getArticle(articleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章";
      });
    },
    handleGetInfo(row) {
      this.$router.push({ path: "/article/edit", query: { articleId: row.articleId } });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.articleId != undefined) {
            updateArticle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }else {
            addArticle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const articleIds = row.articleId || this.ids;
      this.$modal.confirm('是否确认删除文章编号为"' + articleIds + '"的数据项？').then(function() {
        return delArticle(articleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('article/article/export', {
        ...this.queryParams
      }, `article_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.dialog-with-background {
  background-image: url('../../../assets/images/edit3.jpg');
  background-size: cover;
}

.editor {
  background-color: rgba(255, 255, 255, 0.5); /* 使用 rgba() 函数设置背景颜色的透明度 */
  opacity: 1; /* 背景透明度，如果希望同时将内容也设置为半透明，请将此属性设置为 0.5 */
}

</style>
